using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 向量的点乘
/// </summary>
public class Lecture02 : MonoBehaviour
{
    // 两向量点乘，得到两向量之间的夹角，可以得到这两个物体之间的位置关系

    public Transform role;

    public Transform enemy;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    /// <summary>
    /// 检测位置(前、后)
    /// </summary>
    public void CheckEnemy()
    {
        if (this.GetDot() > 0)
        {
            Debug.LogError("敌人在前方");
        }
        else
        {
             Debug.LogError("敌人在后方");
        }
    }

    private float GetDot()
    {
        float dotVal = Vector3.Dot(GetRoleV3(),GetEnemyV3());
        return dotVal;
    }

    /// <summary>
    /// 玩家
    /// </summary>
    /// <returns></returns>
    private Vector3 GetRoleV3()
    {
        if (role)
        {
            Vector3 roleV3 = role.transform.forward - role.transform.position;
            return roleV3;
        }
        return Vector3.one;
    }

    /// <summary>
    /// 敌人
    /// </summary>
    /// <returns></returns>
     private Vector3 GetEnemyV3()
    {
        if (role && enemy)
        {
            Vector3 roleV3 = enemy.transform.position - role.transform.position;
            return roleV3;
        }
        return Vector3.one;
    }
}
